<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :before-close="closeHandle"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="图片" prop="imgurl">
      <el-upload
        accept="image/jpeg,image/gif,image/png,image/bmp"
        :action="url"
        :before-upload="beforeUploadHandle"
        :on-success="successHandle"
        :on-remove="handleRemove"
        :on-exceed="limit"
        :multiple="false"
        :file-list="fileList"
        :limit="1"
        list-type="picture-card">
        <i class="el-icon-plus"></i>
      </el-upload>
    </el-form-item>
    <el-form-item label="标题" prop="title">
      <el-input v-model="dataForm.title" placeholder="标题"></el-input>
    </el-form-item>
    <el-form-item label="描述" prop="description">
      <el-input v-model="dataForm.description" placeholder="描述"></el-input>
    </el-form-item>
    <el-form-item label="价格" prop="price">
      <el-input v-model="dataForm.price" placeholder="价格"></el-input>
    </el-form-item>
    <el-form-item label="显示" prop="isshow">
      <el-radio-group v-model="dataForm.isshow">
        <el-radio :label="0">上架</el-radio>
        <el-radio :label="1">下架</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="排序" prop="sort">
      <el-input v-model="dataForm.sort" placeholder="排序"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false, fileList=[]">取消</el-button>
      <el-button type="primary" :disabled="subable" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        subable: false,
        visible: false,
        url: '',
        num: 0,
        successNum: 0,
        fileList: [],
        filename: '',
        dataForm: {
          id: 0,
          imgurl: '',
          title: '',
          description: '',
          price: '',
          isshow: 0,
          sort: 0
        },
        dataRule: {
          imgurl: [
            { required: true, message: '图片不能为空', trigger: 'blur' }
          ],
          title: [
            { required: true, message: '标题不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '价格不能为空', trigger: 'blur' }
          ],
          isshow: [
            { required: true, message: '是否显示(0:显示；1:不显示)不能为空', trigger: 'blur' }
          ],
          sort: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.url = this.$http.adornUrl(`/breakfast/breakfast/upload?token=${this.$cookie.get('token')}`)
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.subable = false
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/breakfast/breakfast/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.fileList = eval(data.breakfast.imgurl)
                this.dataForm.title = data.breakfast.title
                this.dataForm.description = data.breakfast.description
                this.dataForm.price = data.breakfast.price
                this.dataForm.isshow = data.breakfast.isshow
                this.dataForm.sort = data.breakfast.sort
              }
            })
          }
        })
      },
      // 上传之前
      beforeUploadHandle (file) {
        if (file.type !== 'image/jpg' && file.type !== 'image/jpeg' && file.type !== 'image/png' && file.type !== 'image/gif') {
          this.$message.error('只支持jpg、png、gif格式的图片！')
          return false
        }
        this.num++
      },
      // 上传成功
      successHandle (response, file, fileList) {
        this.successNum++
        if (response && response.code === 0) {
          if (this.num === this.successNum) {
            this.fileList = fileList
          }
        } else {
          this.$message.error(response.msg)
        }
      },
      handleRemove (file, fileList) {
        this.filename = ''
        if (file.response !== undefined) {
          this.filename = file.response.name
        } else {
          this.filename = file.name
        }
        this.$http({
          url: this.$http.adornUrl(`/breakfast/breakfast/picdelete`),
          method: 'post',
          data: this.$http.adornData({
            name: this.filename
          })
        })
        this.fileList = fileList
      },
      limit () {
        this.$message.error('超出文件上传个数')
      },
      // 弹窗关闭时
      closeHandle () {
        this.fileList = []
        this.$emit('refreshDataList')
        this.visible = false
      },
      // 表单提交
      dataFormSubmit () {
        this.subable = true
        this.dataForm.imgurl = ''
        if (this.fileList !== undefined) {
          for (var i = 0; i < this.fileList.length; i++) {
            if (this.fileList[i].response !== undefined) {
              this.fileList[i].name = this.fileList[i].response.name
            }
            this.dataForm.imgurl += this.fileList[i].name + ','
          }
          if (this.dataForm.imgurl !== '') {
            this.dataForm.imgurl = this.dataForm.imgurl.substr(0, this.dataForm.imgurl.length - 1)
          }
        }
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/breakfast/breakfast/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'imgurl': this.dataForm.imgurl,
                'title': this.dataForm.title,
                'description': this.dataForm.description,
                'price': this.dataForm.price,
                'isshow': this.dataForm.isshow,
                'sort': this.dataForm.sort
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1000,
                  onClose: () => {
                    this.fileList = []
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          } else {
            this.subable = false
          }
        })
      }
    }
  }
</script>
